<?php
require './model/Db.php';
require './model/Page.php';
$Db = Db::getInstance();
$user_data = $Db->islogin();
function job($job){
	if($job == 3){
		$job = '站务';
	}elseif($job == 2){
		$job = '区务';
	}elseif($job == 1){
		$job = '版务';
	}else{
		$job = '普通用户';
	}
	return $job;
}
function lv($lv){
	//等级
	if($lv>=700){
		$lv = 'lv-7';
	}elseif($lv>=580){
		$lv = 'lv-6';
	}elseif($lv>=460){
		$lv = 'lv-5';
	}elseif($lv>=340){
		$lv = 'lv-4';
	}elseif($lv>=220){
		$lv = 'lv-3';
	}elseif($lv>=100){
		$lv = 'lv-2';
	}else{
		$lv = 'lv-1';
	}
	return $lv;
}
$k_arr = ['版面','帖子','用户'];
$data = [];
if($_GET['wd']){
	switch ($_GET['k']) {
		case '帖子':
			$table = 'content';
			$count = $Db->count([
				table => [$table],
				where => "cid = 0 and title like '%{$_GET['wd']}%'"
			]);
			$page = page($count,PAGE_SIZE,PAGE_BTN_CNT);
			$where = "cid = 0 and title like '%{$_GET['wd']}%' {$page['limit']}";
			$sign = "帖子";
			break;

		case '用户':
			$table = 'bbs_user';
			$count = $Db->count([
				table => [$table],
				where => "user like '%{$_GET['wd']}%'"
			]);
			$page = page($count,PAGE_SIZE,PAGE_BTN_CNT);
			$where = "user like '%{$_GET['wd']}%' {$page['limit']}";
			$sign = "用户";
			break;

		default:
			$table = 'module';
			$count = $Db->count([
				table => [$table],
				where => "name like '%{$_GET['wd']}%'"
			]);
			$page = page($count,PAGE_SIZE,PAGE_BTN_CNT);
			$where = "name like '%{$_GET['wd']}%' {$page['limit']}";
			$sign = "版面";
			break;

	}
	$data = $Db->find([
		table => [$table],
		where => $where
	]);
	$user_list = [];
 	$m_list = [];
 	$c_list = [];
 	switch ($_GET['k']) {
 		case '帖子':
	foreach ($data as $item) {
		$item['user_info'] = $Db->find([
			table => ['bbs_user'],
			field => 'id,user,photo',
			where => "id = {$item['uid']}"
		])[0];
		$c_list[] = $item;
	}
		$c_list = $Db->tohtml($c_list);
			break;

		case '用户':
		if($user_data){
			$uids_arr = json_decode($user_data['my_follow'])->uids;
		}
	foreach ($data as $item) {
		//获取关注和粉丝
		$my_follow = json_decode($item['my_follow'])->uids;
		$follow_me = json_decode($item['follow_me'])->uids;
		$item['follow'] = count($my_follow);
		$item['fans'] = count($follow_me);
		$item['job'] = job($item['job']);
		$item['lv'] = lv($item['lv']);
		//获取发帖数
		$item['count'] = $Db->count([
			table => ['content'],
			where => "`uid` = {$item['id']}"
		]);
		//获取我的关注
		if($user_data){
			$item['fol_class'] = in_array($item['id'], $uids_arr)?'followed':'';
			$item['fol_title'] = in_array($item['id'], $uids_arr)?'已关注':'未关注';
		}
		//状态
		$item['stat'] = (time()-$item['last_time'])<1800 ? 'online':'offline';
		$item['last_time'] = date('Y-m-d H:i:s',$item['last_time']);
		$user_list[] = $item;
	}
		$user_list = $Db->tohtml($user_list);
			break;

		default:
	foreach ($data as $item) {
		//版务
		$manager = json_decode($item['manager'])->uids;
		$uids = implode(',', $manager);
		if(!$uids){
			$uids = -1;
		}
		$manager_info = $Db->find([
			table => ['bbs_user'],
			field => 'id,user',
			where => "id in ({$uids})"
		]);
		$item['manager'] = $manager_info;
		//今日
		$item['today'] = $Db->count([
			table => [content => 'a',module => 'b'],
			where => "a.mid = b.id and b.id = {$item['id']} and a.time > CURDATE()"
		]);
		//主题
		$item['main'] = $Db->count([
			table => [content => 'a',module => 'b'],
			where => "a.mid = b.id and b.id = {$item['id']} and a.cid = 0"
		]);
		//总数
		$item['all'] = $Db->count([
			table => [content => 'a',module => 'b'],
			field => 'a.title,a.time,a.uid,a.id,a.cid',
			where => "a.mid = b.id and b.id = {$item['id']} order by a.time desc"
		]);
		$all_cont = $Db->find([
			table => [content => 'a',module => 'b'],
			field => 'a.title,a.time,a.uid,a.id,a.cid',
			where => "a.mid = b.id and b.id = {$item['id']} and a.delete_time is NULL order by a.time desc limit 0,1"
		])[0];
		if($all_cont){
			$user_info = $Db->find([
				table => ['bbs_user'],
				field => 'id,photo,user',
				where => "id = {$all_cont['uid']}"
			])[0];
			$all_cont['user_info'] = $user_info;
		}
		$item['content'] = $all_cont;
		$m_list[] = $item;
	}
		$m_list = $Db->tohtml($m_list);
			break;
 	}
	
}

$web_title = '搜索'.$sign;
$css = [
	'public/css/my-bootstrap.min.css',
	'public/css/search.pc.min.css'
];
include './view/header.html';
include './view/search.html';
include './view/footer.html';